

//
//  ShowView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/30.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ShowView.h"
#import "ShowCollectionCell.h"
#import "UIView+ZF.h"
#import "ReleaseModel.h"
#define kRowH 44
@interface ShowView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,ShowCollectionCellDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong) UICollectionView *collectionView;
//@property(nonatomic,assign) NSInteger indexShow;
//@property (nonatomic, strong) UIView *shadow;
@property(nonatomic,assign) NSInteger preRow;
@property(nonatomic,strong) ShowCollectionCell *preCell;

@end
@implementation ShowView
static NSString *identifier = @"ShowCollectionCell";
static NSString *identifierHeaderView = @"headerView";
//-(NSMutableArray *)demandArray
//{
//    if (!_demandArray) {
//        _demandArray = [NSMutableArray array];
//    }
//    return _demandArray;
//}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        XLog(@"[self maxCollectionListHeightWithModel:self.dataMuta]:%f",[self maxCollectionListHeightWithModel:self.dataMuta]);
        UICollectionViewFlowLayout *flowe = [[UICollectionViewFlowLayout alloc]init];
        CGRect frame = CGRectMake(0.f, 0, self.w, 0);
        flowe.minimumLineSpacing = 0;
        flowe.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowe];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ShowCollectionCell class] forCellWithReuseIdentifier:identifier];
        

        
    }
    return _collectionView;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        self.shadow = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth,50)];
//        XLog(@"self.shadow:%@",NSStringFromCGRect(self.shadow.frame));
//        self.shadow.backgroundColor = [UIColor whiteColor];
//        [self addSubview:self.shadow];
        XLog(@"dataMuta:%ld",self.dataMuta.count);
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
//        self.backgroundColor = [UIColor whiteColor];
        self.preRow = 0;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shadowViewClick)];
        tap.delegate= self;
        [self addGestureRecognizer:tap];
    }
    return self;
}
-(void)setDataMuta:(NSMutableArray *)dataMuta
{
    _dataMuta = dataMuta;

    [self addSubview:self.collectionView];
    [self.collectionView reloadData];
    XLog(@"大类搜索:%ld",self.dataMuta.count);
}
#pragma mark - 点击背景
- (void)shadowViewClick
{
    [self hideListViewAnimation];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    XLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if([touch.view isDescendantOfView:self.collectionView]){
        return NO;
    }
    [self hideListViewAnimation];
    return  YES;
}
// 隐藏下拉列表
- (void)hideListViewAnimation;
{
    [UIView animateWithDuration:0.3 animations:^{
        self.collectionView.h = 0;
    } completion:^(BOOL finished) {
    }];
    if ([self.delegate respondsToSelector:@selector(showView:withShow:)]) {
        [self.delegate showView:self withShow:NO];
    }
}
// 更具数据源里边数据情况，返回TableView高度
- (CGFloat)maxCollectionListHeightWithModel:(NSArray *)dataSource
{
    //    CGFloat height = (dataSource.count /3+1)*kRowH;
    CGFloat height = (dataSource.count/3+1)*kRowH+10+50; 
    XLog(@"height:%f",height);
    return height;
}
// 显示下拉列表
- (void)showListViewAnimation
{
    
    [UIView animateWithDuration:0.3 animations:^{
//        self.alpha = 1.f;
        self.collectionView.h = [self maxCollectionListHeightWithModel:self.dataMuta];
    }];
    ReleaseModel *model = self.dataMuta[self.defaultRow];
    ReleaseModel *modelPre = self.dataMuta[self.preRow];
    if (self.preRow == self.defaultRow) {
        model.show = YES;
    }else {
        model.show = YES;
        modelPre.show = NO;
    }
    self.preRow = self.defaultRow;
    [self.collectionView reloadData];

}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataMuta.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShowCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    ReleaseModel *model = self.dataMuta[indexPath.row];
    cell.delegate = self;
    cell.indexPath = indexPath;
    cell.releaseModel = model;
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierHeaderView];
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierHeaderView forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc]initWithFrame:reusableView.bounds];
        [reusableView addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = self.title;
        label.textColor = kLabel16Color;
        label.font = [UIFont systemFontOfSize:18];
        return reusableView;
    }else {
        return nil;
    }
}
- (void)setTitle:(NSString *)title
{
    _title = title;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kWidth, 50);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kWidth-3)/3, kRowH);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XLog(@"indexPath.row:%ld",indexPath.row);
    XLog(@"self.preRow:%ld",self.preRow);
    ReleaseModel *model = self.dataMuta[indexPath.row];
    ReleaseModel *modelPre = self.dataMuta[self.preRow];
    if (self.preRow == indexPath.row) {
        model.show = YES;
    }else {
        model.show = YES;
        modelPre.show = NO;
    }
    self.preRow = indexPath.row;
    XLog(@"model.show:%d",model.show);
    XLog(@"modelPre.show:%d",modelPre.show);
    if ([self.delegate respondsToSelector:@selector(showView:withTitle:withRow:withShow:)]) {
        [self.delegate showView:self withTitle:model.content withRow:indexPath.row+1 withShow:YES];
    }
    [self hideListViewAnimation];
//    [self.collectionView reloadData];

}

@end
